﻿Imports System.Data
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Data.OleDb
Imports System
Imports System.Globalization

Partial Class Interfaz_frmAdminGuiaDespacho
    Inherits System.Web.UI.Page
    Dim CL As New Logica.ModuloAdministracion.ControladorLogica
    Dim dtDatos As New DataTable
    Dim dataset As New DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CL = Logica.ModuloAdministracion.ControladorLogica.getInstance

        If CL.visibilidadesFormsInternos("Administracion", Session("user").ToString) = False Then
            Response.Redirect("Index.aspx")
        End If
        Dim i As Integer
        Dim ArrayCliente As New ArrayList
        Dim ArrayMaterial As New ArrayList
        Dim ArrayOrdenCompra As New ArrayList
        Dim ArrayRequisicion As New ArrayList

        ArrayCliente = CL.obtenerCliente()
        ArrayMaterial = CL.ObtenerMaterial()
        ArrayOrdenCompra = CL.ObtenerOrdenCompra()
        ArrayRequisicion = CL.ObtenerRequisicion()

        If Not Page.IsPostBack Then
            For i = 0 To ArrayCliente.Count - 1
                Me.DropCliente.Items.Add(DirectCast(ArrayCliente.Item(i), Cliente).RznSocial)
            Next

            For i = 0 To ArrayMaterial.Count - 1
                Me.DropItem.Items.Add(DirectCast(ArrayMaterial.Item(i), Material).Nombre)
            Next

            For i = 0 To ArrayOrdenCompra.Count - 1
                Me.DropOrdenCompra.Items.Add(DirectCast(ArrayOrdenCompra.Item(i), OrdenCompra).NumeroDeOrden)
            Next

            For i = 0 To ArrayRequisicion.Count - 1
                Me.DropRequisicion.Items.Add(DirectCast(ArrayRequisicion.Item(i), Requisicion).Id)
            Next
        End If

        'se guardan datos en secion para el positback de la pagina respecto al los datos del datagridview
        If Session("GuiaDespacho") Is Nothing Then
            Session("GuiaDespacho") = dtDatos
        Else
            dtDatos = DirectCast(Session("GuiaDespacho"), DataTable)

        End If

        If Not Page.IsPostBack Then
            Try
                dtDatos.Columns.Add(New DataColumn("ITEM"))
                dtDatos.Columns.Add(New DataColumn("CODIGO"))
                dtDatos.Columns.Add(New DataColumn("DETALLE"))
                dtDatos.Columns.Add(New DataColumn("CANTIDAD"))
                dtDatos.Columns.Add(New DataColumn("PRECIOUNITARIO"))

                Me.GridGuiaDespacho.DataBind()
                Me.GridGuiaDespacho.DataSource = Me.dtDatos

            Catch
            End Try
        End If
        'Me.GridGuiaDespacho.DataBind()
        Me.GridGuiaDespacho.DataSource = Me.dtDatos
    End Sub

#Region "Funcionalidades"
    Public Sub Mensaje(ByVal msg As String)
        Dim alertScript As String = "<script language=JavaScript runat=server>"
        alertScript &= "alert('" & msg & "');"
        alertScript &= "</script>"
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScrip", alertScript)
    End Sub

    Private Sub LimpiarItem()
        TxtPrecioUnitario.Text = String.Empty
        TxtCodItem.Text = String.Empty
        TxtCantidad.Text = String.Empty
        TxtDescripcion.Text = String.Empty
    End Sub

    Private Sub LimpiarTodo()
        TxtPrecioUnitario.Text = String.Empty
        TxtCodItem.Text = String.Empty
        TxtCantidad.Text = String.Empty
        TxtDescripcion.Text = String.Empty
        TxtCondVenta.Text = String.Empty
    End Sub

    Private Sub InsertarGuiaDespacho()
        Try
            Dim drDatos As DataRow = dtDatos.NewRow

            If Me.TxtCodItem.Text <> "" And Me.TxtDescripcion.Text <> _
            "" And Me.TxtCantidad.Text <> "" And Me.TxtPrecioUnitario.Text <> "" Then

                drDatos.Item("ITEM") = Me.DropItem.Text
                drDatos.Item("CODIGO") = Me.TxtCodItem.Text
                drDatos.Item("DETALLE") = Me.TxtDescripcion.Text
                drDatos.Item("CANTIDAD") = Me.TxtCantidad.Text
                drDatos.Item("PRECIOUNITARIO") = CInt(Me.TxtPrecioUnitario.Text)

                dtDatos.Rows.Add(drDatos)

                Me.GridGuiaDespacho.DataSource = dtDatos
                Me.GridGuiaDespacho.DataBind()

                Me.LimpiarItem()
                Me.GridGuiaDespacho.ForeColor = Drawing.Color.Blue
                Me.LabelAviso.Text = "Los datos han sido ingresados satisfactoriamente"
            Else
                Me.LabelAviso.ForeColor = Drawing.Color.Red
                Me.LabelAviso.Text = "Complete todos los datos"
            End If

        Catch ex As Exception
            Me.Mensaje(ex.ToString)
        End Try
    End Sub

#End Region

#Region "Botones"

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.LimpiarTodo()
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        InsertarGuiaDespacho()
    End Sub
#End Region

#Region "Eventos Grid"
    'Grid Material
    Protected Sub GridGuiaDespacho_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        GridGuiaDespacho.PageIndex = e.NewPageIndex
        'Bind data to the GridView control.
        GridGuiaDespacho.DataSource = Session("GuiaDespacho")
        GridGuiaDespacho.DataBind()
    End Sub

    Protected Sub GridGuiaDespacho_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        'Set the edit index.
        GridGuiaDespacho.EditIndex = e.NewEditIndex
        'Bind data to the GridView control.
        GridGuiaDespacho.DataSource = Session("GuiaDespacho")
        GridGuiaDespacho.DataBind()
    End Sub

    Protected Sub GridGuiaDespacho_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
        'Reset the edit index.
        GridGuiaDespacho.EditIndex = -1
        'Bind data to the GridView control.
        GridGuiaDespacho.DataSource = Session("GuiaDespacho")
        GridGuiaDespacho.DataBind()
    End Sub

    Protected Sub GridGuiaDespacho_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        'Retrieve the table from the session object.
        Dim dt As DataTable = CType(Session("GuiaDespacho"), DataTable)

        'Update the values.
        Dim row As GridViewRow = GridGuiaDespacho.Rows(e.RowIndex)
        'Dim actividad As String = (CType((row.Cells(2).Controls(0)), TextBox)).Text.ToString
        Dim CODIGO As String = (CType((row.Cells(2).Controls(0)), TextBox)).Text
        Dim DETALLE As String = (CType((row.Cells(3).Controls(0)), TextBox)).Text
        Dim CANTIDAD As String = (CType((row.Cells(4).Controls(0)), TextBox)).Text
        Dim PRECIOUNITARIO As String = (CType((row.Cells(5).Controls(0)), TextBox)).Text

        'dt.Rows(row.DataItemIndex)("ITEM") = actividad
        dt.Rows(row.DataItemIndex)("CODIGO") = CODIGO
        dt.Rows(row.DataItemIndex)("DETALLE") = DETALLE
        dt.Rows(row.DataItemIndex)("CANTIDAD") = CANTIDAD
        dt.Rows(row.DataItemIndex)("PRECIOUNITARIO") = PRECIOUNITARIO

        'Reset the edit index.
        GridGuiaDespacho.EditIndex = -1

        'Bind data to the GridView control.
        GridGuiaDespacho.DataSource = Session("GuiaDespacho")
        GridGuiaDespacho.DataBind()
    End Sub

    Sub GridGuiaDespacho_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        Dim dt As DataTable = CType(Session("GuiaDespacho"), DataTable)
        dt.Rows(e.RowIndex).Delete()
        GridGuiaDespacho.DataSource = dt
        GridGuiaDespacho.DataBind()
    End Sub
#End Region

End Class
